package Service;

import Bean.Page;
import Bean.User;
import Dao.UserDao;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-03
 * Time: 9:59
 */
public class UserService {
    private UserDao userDao = new UserDao();


    public User Login(User user) {
        //1.先根据手机号码查询用户数据
        User user1 = userDao.selectByPhone(user.getPhone());

        if(user1 == null) {
            return null;
        }
        if(user.getPhone().equals(user1.getPhone())) {
            return user1;
        }
        return null;
    }

    public Page select(int pageNo, int pageSize) {
        Page page = new Page();
        page.setTotalRecords(userDao.UserSize());
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        //填入pagoNo页对应数据
        page.setList(userDao.select(pageNo,pageSize));
        return page;
    }

    public boolean deleteById(String userId) {
        User user = new User();
        user.setUserId(userId);
        return userDao.delete(user);
    }

    public boolean deleteByIdS(String[] UserIds) {
        for (String UserId:UserIds) {
            boolean ok = deleteById(UserId);
            if(!ok) {
                return false;
            }
        }
        return true;
    }

    public Page selectByName(int pageNo, int pageSize, String userName) {
        Page page = new Page();
        page.setTotalRecords(userDao.UserByNameSize(userName));
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        //填入pagoNo页对应数据
        page.setList(userDao.selectByName(pageNo,pageSize,userName));
        return page;
    }

    //注册用户
    public boolean insert(User user) {
        return userDao.insert(user);
    }

    public User selectById(String userId) {
        return userDao.selectById(userId);
    }

    public boolean update(User user) {
        return userDao.update(user);
    }
}
